package jp.co.iris_ltd.irisphotoeditor;

import jp.co.iris_ltd.irisphotoeditor.util.PenArtTool;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;

public class PentoolPalette extends LinearLayout implements View.OnClickListener {
    private SparseArray<View> mStyles = new SparseArray<View>();
    private SparseArray<View> mColors = new SparseArray<View>();
    private Paint mOutlinePaint = new Paint();
    private Paint mInlinePaint = new Paint();

    public static Paint getDefaultInlinePaint() {
        Paint inlinePaint = new Paint();
        inlinePaint.setColor(PenArtTool.INLINE_DEFAULT_COLOR);
        inlinePaint.setStyle(PenArtTool.INLINE_DEFAULT_STYLE);
        inlinePaint.setAntiAlias(PenArtTool.INLINE_DEFAULT_ANTI_ALIAS);
        inlinePaint.setStrokeWidth(PenArtTool.INLINE_DEFAULT_STROKE_WIDTH);
        inlinePaint.setStrokeCap(PenArtTool.INLINE_DEFAULT_CAP);
        inlinePaint.setStrokeJoin(PenArtTool.INLINE_DEFAULT_JOIN);
        return inlinePaint;
    }

    public static Paint getDefaultOutlinePaint() {
        Paint outlinePaint = new Paint();
        outlinePaint.setColor(PenArtTool.OUTLINE_DEFAULT_COLOR);
        outlinePaint.setStyle(PenArtTool.OUTLINE_DEFAULT_STYLE);
        outlinePaint.setAntiAlias(PenArtTool.OUTLINE_DEFAULT_ANTI_ALIAS);
        outlinePaint.setStrokeWidth(PenArtTool.OUTLINE_DEFAULT_STROKE_WIDTH);
        outlinePaint.setStrokeCap(PenArtTool.OUTLINE_DEFAULT_CAP);
        outlinePaint.setStrokeJoin(PenArtTool.OUTLINE_DEFAULT_JOIN);
        return outlinePaint;
    }

    public PentoolPalette(Context context) {
        super(context);
    }

    public PentoolPalette(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // public void getPaint(Paint inlinePaint, Paint outlinePaint) {
    // inlinePaint = new Paint( mInlinePaint);
    // outlinePaint = new Paint( mOutlinePaint);
    // }

    public Paint getInlinePaint() {
        return mInlinePaint;
    }

    public Paint getOutlinePaint() {
        return mOutlinePaint;
    }

    public void init() {
        mInlinePaint = getDefaultInlinePaint();
        mOutlinePaint = getDefaultOutlinePaint();

        mStyles.put(R.id.imgPenStyle01, findViewById(R.id.imgPenStyle01));
        mStyles.put(R.id.imgPenStyle02, findViewById(R.id.imgPenStyle02));
        mStyles.put(R.id.imgPenStyle03, findViewById(R.id.imgPenStyle03));
        mStyles.put(R.id.imgPenStyle04, findViewById(R.id.imgPenStyle04));
        mStyles.put(R.id.imgPenStyle05, findViewById(R.id.imgPenStyle05));
        mStyles.put(R.id.imgPenStyle06, findViewById(R.id.imgPenStyle06));

        mColors.put(R.id.imgPenColorBlack, findViewById(R.id.imgPenColorBlack));
        mColors.put(R.id.imgPenColorBlue, findViewById(R.id.imgPenColorBlue));
        mColors.put(R.id.imgPenColorBrawn, findViewById(R.id.imgPenColorBrawn));
        mColors.put(R.id.imgPenColorFlesh, findViewById(R.id.imgPenColorFlesh));
        mColors.put(R.id.imgPenColorGreen, findViewById(R.id.imgPenColorGreen));
        mColors.put(R.id.imgPenColorLightBlue, findViewById(R.id.imgPenColorLightBlue));
        mColors.put(R.id.imgPenColorLightGreen, findViewById(R.id.imgPenColorLightGreen));
        mColors.put(R.id.imgPenColorOrange, findViewById(R.id.imgPenColorOrange));
        mColors.put(R.id.imgPenColorPaleYellow, findViewById(R.id.imgPenColorPaleYellow));
        mColors.put(R.id.imgPenColorPink, findViewById(R.id.imgPenColorPink));
        mColors.put(R.id.imgPenColorPurple, findViewById(R.id.imgPenColorPurple));
        mColors.put(R.id.imgPenColorRed, findViewById(R.id.imgPenColorRed));
        mColors.put(R.id.imgPenColorThintLightBlue, findViewById(R.id.imgPenColorThintLightBlue));
        mColors.put(R.id.imgPenColorUltramarinered, findViewById(R.id.imgPenColorUltramarinered));
        mColors.put(R.id.imgPenColorWhite, findViewById(R.id.imgPenColorWhite));
        mColors.put(R.id.imgPenColorYellow, findViewById(R.id.imgPenColorYellow));

        for (int i = 0; i < mStyles.size(); i++) {
            mStyles.valueAt(i).setOnClickListener(this);
        }
        for (int i = 0; i < mColors.size(); i++) {
            mColors.valueAt(i).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Resources res = getResources();
        switch (id) {
        case R.id.imgPenColorBlack:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_black));
            break;
        case R.id.imgPenColorBlue:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_blue));
            break;
        case R.id.imgPenColorBrawn:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_brawn));
            break;
        case R.id.imgPenColorFlesh:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_flesh));
            break;
        case R.id.imgPenColorGreen:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_green));
            break;
        case R.id.imgPenColorLightBlue:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_light_blue));
            break;
        case R.id.imgPenColorLightGreen:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_light_green));
            break;
        case R.id.imgPenColorYellow:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_yellow));
            break;
        case R.id.imgPenColorOrange:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_orange));
            break;
        case R.id.imgPenColorPaleYellow:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_pale_yellow));
            break;
        case R.id.imgPenColorPink:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_pink));
            break;
        case R.id.imgPenColorPurple:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_purple));
            break;
        case R.id.imgPenColorRed:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_red));
            break;
        case R.id.imgPenColorThintLightBlue:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_thin_light_blue));
            break;
        case R.id.imgPenColorUltramarinered:
            mOutlinePaint.setColor(res.getColor(R.color.color_white));
            mInlinePaint.setColor(res.getColor(R.color.color_ultramarine));
            break;
        case R.id.imgPenColorWhite:
            mOutlinePaint.setColor(res.getColor(R.color.color_black));
            mInlinePaint.setColor(res.getColor(R.color.color_white));
            break;
        case R.id.imgPenStyle01:
            mOutlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_01_size) + getResources().getDimensionPixelSize(R.dimen.pen_color_stroke));
            mInlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_01_size));
            break;
        case R.id.imgPenStyle02:
            mOutlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_02_size) + getResources().getDimensionPixelSize(R.dimen.pen_color_stroke));
            mInlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_02_size));
            break;
        case R.id.imgPenStyle03:
            mOutlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_03_size) + getResources().getDimensionPixelSize(R.dimen.pen_color_stroke));
            mInlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_03_size));
            break;
        case R.id.imgPenStyle04:
            mOutlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_04_size) + getResources().getDimensionPixelSize(R.dimen.pen_color_stroke));
            mInlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_04_size));
            break;
        case R.id.imgPenStyle05:
            mOutlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_05_size) + getResources().getDimensionPixelSize(R.dimen.pen_color_stroke));
            mInlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_05_size));
            break;
        case R.id.imgPenStyle06:
            mOutlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_06_size) + getResources().getDimensionPixelSize(R.dimen.pen_color_stroke));
            mInlinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.pen_style_06_size));
            break;
        default:
            return;
        }

        if (mStyles.get(v.getId()) != null) {
            for (int i = 0; i < mStyles.size(); i++) {
                mStyles.valueAt(i).setBackgroundDrawable(null);
            }
            v.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_pen_selected));
        } else if (mColors.get(v.getId()) != null) {
            for (int i = 0; i < mColors.size(); i++) {
                mColors.valueAt(i).setBackgroundDrawable(null);
            }
            v.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_pen_selected));
        }
    }
}
